// pages/index/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 'tab1',
    staticUrl: '',
    apiUrl: '',
    date: '请选择时间',
    user:[],
    checkStatus:1
  },

  handleChange({ detail }) {
    if (this.data.date == '请选择时间'){
      wx.showToast({
        title: '未选择时间',
        icon:'none',
        duration:1500
      })
      return false;
    }
    if (detail.key == "tab1"){
      this.setData({
        current: detail.key,
        checkStatus: 1
      });
    } else if (detail.key == "tab2") {
      this.setData({
        current: detail.key,
        checkStatus: 2
      });
    } else if (detail.key == "tab3") {
      this.setData({
        current: detail.key,
        checkStatus: 3
      });
    } else if (detail.key == "tab4") {
      this.setData({
        current: detail.key,
        checkStatus: 4
      });
    }

    // 发起网络请求
    var userinfo = wx.getStorageSync("userInfo");
    wx.request({
      url: this.data.apiUrl + '/coach/selectappointment.do',
      data: {
        status: userinfo.status,
        session_key: wx.getStorageSync("session_key"),
        openid: wx.getStorageSync("openid"),
        checkStatus: this.data.checkStatus,
        cid: userinfo.cid,
        date: this.data.date
      },
      method: 'GET',
      success: (res) => {
        console.log(res, '获取工单')
        this.setData({
          user: res.data.appointments
        })
      }
    })
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
    this.requestTolast();
  },
  requestTolast:function(){
    // 发起网络请求
    var userinfo = wx.getStorageSync("userInfo");
    wx.request({
      url: this.data.apiUrl + '/coach/selectappointment.do',
      data: {
        status: userinfo.status,
        session_key: wx.getStorageSync("session_key"),
        openid: wx.getStorageSync("openid"),
        checkStatus: this.data.checkStatus,
        cid: userinfo.cid,
        date: this.data.date
      },
      method: 'GET',
      success: (res) => {
        console.log(res, '获取工单')
        this.setData({
          user: res.data.appointments
        })
      }
    })
  },
  updateCheckStatus:function(e){
    // 发起网络请求
    var userinfo = wx.getStorageSync("userInfo");
    wx.request({
      url: this.data.apiUrl + '/coach/updateCheckStatus.do',
      data: {
        status: userinfo.status,
        session_key: wx.getStorageSync("session_key"),
        openid: wx.getStorageSync("openid"),
        reservationId: e.currentTarget.dataset.id,
        checkStatus: e.currentTarget.dataset.flag
      },
      method: 'GET',
      success: (res) => {
        console.log(res, '修改状态');
        wx.showToast({
          title: '确认成功',
          icon:'success',
          duration:1500
        })

        setTimeout(()=>{
          this.requestTolast();
        },1500)
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      staticUrl: app.globalData.staticUrl,
      apiUrl: app.globalData.apiUrl
    })
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    var day = myDate.getDate();
    if (month > 9){
      this.setData({
        date: year + '-' + month + '-' + day
      })
    }else{
      this.setData({
        date: year + '-0' + month + '-' + day
      })
    }
    this.requestTolast();

  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})